package cn.itlaobing;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import cn.itlaobing.entity.Student;

public class Main {

	public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
		
		Class cls=Student.class;
		
		//取得所有定义的field
		Field[] fields=cls.getDeclaredFields();
		
		Method[] methods=cls.getDeclaredMethods();
		
		for (Field field : fields) {
			System.out.println(field.getName());
		}
		System.out.println(">>>>>>>>>>>>>>>>>>>>");
		for (Method method : methods) {
			
			System.out.println(method.getName());
		}
		
		//创建对象
		Student stu=(Student) cls.newInstance();
		//取得私有的feild
		Field nameField=cls.getDeclaredField("name");
		//将这个私有的field设置为可访问
		nameField.setAccessible(true);
		nameField.set(stu, "张三");
		

		System.out.println(stu.getName());
	
		Method getNameMethod=cls.getDeclaredMethod("test");
		//调用方法
		String result=(String)getNameMethod.invoke(stu);
		getNameMethod.setAccessible(true);
		System.out.println(result);
		
	

	}

}
